#ifndef WIDGET_H
#define WIDGET_H

#include "viewswitchbutton.h"
#include "searchlistwidget.h"
#include "songitem.h"
#include "volumetoolwidget.h"
#include "playpagewidget.h"
#include <QWidget>
#include <QVector>
#include <QMediaPlayer>
//#include <QMediaPlaylist>
#include <QtMultimedia/QMediaPlayer>
#include<QList>
#include<QUrl>
#include <QElapsedTimer>

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QPropertyAnimation>
#include<QAudioOutput>

#include <QSystemTrayIcon>
QT_BEGIN_NAMESPACE
namespace Ui { class MusicPlayer; }
QT_END_NAMESPACE

class MusicPlayer : public QWidget
{
    Q_OBJECT

public:
    MusicPlayer(QWidget *parent = nullptr);
    ~MusicPlayer();

    void on_songItem_played(const SongItem& songItem, bool play);
    void on_songItem_playAll(const QVector<QUrl>& songs);
    void on_player_stateChanged(QMediaPlayer::PlaybackState state);//修正
    void on_playlist_mediaFirstChanged();
    void on_playlist_currentIndexFChanged(int currentIndex);
    void on_playlist_currentIndexChanged(int currentIndex);
    void on_playlist_playbackModeChanged(int mode);//修正
private slots:
    void on_pushButton_closeWindow_clicked();

    void on_pushButton_fullscreen_clicked();

    void on_pushButton_miniMode_clicked();

    void on_lineEdit_search_returnPressed();

    void on_pushButton_search_clicked();

    void on_pushButton_playPause_clicked(bool checked);

    void on_pushButton_playNext_clicked();

    void on_pushButton_playPrev_clicked();

    void on_pushButton_playbackMode_clicked();

    void on_pushButton_minimize_clicked();

    void on_pushButton_volume_clicked();

    void on_pushButton_like_clicked();

    void on_pushButton_18_clicked();

private:
    void on_viewSwitchButton_clicked(int index);
private:
    // 初始化UI:
    void initUI();
    void initMainWindow();
    void initBodyLeft();
    void allowWindowChangeSize(bool);
    void initPlaybackControlPanel();
    void initSearch();
    void initPlayPage();
    void initTray();

    void exit();

    void beginPlay(QUrl url);
    void stopPlay();
    void updateSongMessage(const SongItem& songItem);
    void updateCurrentSongImage(const QPixmap& image);
    void updateLikeButton(bool isLike);
    void initSongImageTimer();

    void changePlayBackColor(const QString& color);
private:
    // 处理窗口拖动:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    int dx = 0, dy = 0;


    bool eventFilter(QObject *watched, QEvent *event);
private:
    QVector<ViewSwitchButton*> _viewSwitchButtons; // 推荐、精选、关注。。。之类的按钮
    SearchListWidget* _searchList;
    PlayPageWidget* _playPage;

    QMediaPlayer *_player;
    QAudioOutput *audioOutput;
    //QMediaPlaylist *_playlist;
    QVector<QUrl> _currentSongs;

    QUrl _currentUrl;
private:
    VolumeToolWidget* _volumeTool;
    QSystemTrayIcon* _tray;

private:
    QPixmap _currentSongImage;
    QElapsedTimer _songImageElapsedTimer; // 用于精确计算时间,
    QTimer *_songImageTimer = nullptr;
    qreal _songImageRotationAngle = 0.0;
    qint64 _songImageSumPausedTime = 0;
    qint64 _songImageLastPausedTime = 0;

private:
    Ui::MusicPlayer *ui;
};
#endif // WIDGET_H
